package com.easyshopping;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ResourceCursorAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.easyshopping.domain.ShoppingList;
import com.easyshopping.facade.ShoppingListFacade;

public class MainActivity extends Activity implements OnItemClickListener, OnClickListener {
	
	private ListView ls_shoppingLists;
//	private ImageButton btn_addShoppingList;
	private Button btn_addShoppingList;
	private Cursor mCursor_shoppinglist;
//	private ImageButton btn_all;
	private Button btn_all;
	private boolean mShowAll = false;
	
//	private String[] mStrings = {
//            "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi",
//            "Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu", "Airag", "Airedale",
//            "Aisy Cendre", "Allgauer Emmentaler", "Alverca", "Ambert", "American Cheese",
//            "Ami du Chambertin", "Anejo Enchilado", "Anneau du Vic-Bilh"};
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.shopping_lists);
        
        ShoppingListFacade.getInstance().setCtx(this);
        
        ls_shoppingLists = (ListView)this.findViewById(R.id.ShoppingLists);
        fillShoppingLists();
//        ls_shoppingLists.setAdapter(new ArrayAdapter<String>(this, 
//        		android.R.layout.simple_list_item_1, mStrings));
        ls_shoppingLists.setOnItemClickListener(this);
        
//        btn_addShoppingList = (ImageButton)this.findViewById(R.id.AddShoppingList);
        btn_addShoppingList = (Button)this.findViewById(R.id.AddShoppingList);
        btn_addShoppingList.setOnClickListener(this);
        
//        btn_all = (ImageButton)this.findViewById(R.id.btn_all);
        btn_all = (Button)this.findViewById(R.id.btn_all);
        btn_all.setOnClickListener(this);
        
//        ShoppingListFacade.getInstance().getPersistence();
    }

	public void onItemClick(AdapterView<?> l, View v, int position, long id) {
		
		Cursor c = mCursor_shoppinglist;
		c.moveToPosition(position);
		
		ShoppingListFacade.getInstance().showShoppingList(
				c.getInt(c.getColumnIndex(ShoppingList.SL_ID)), 
				c.getString(c.getColumnIndex(ShoppingList.SL_NAME)));
	}

	public void onClick(View view) {
		
		if (view.getId() == R.id.btn_all) {
			
			mShowAll = !mShowAll;
			fillShoppingLists();
			
			if (mShowAll) {
				btn_all.setText(R.string.page_main_button_less);
			} else {
				btn_all.setText(R.string.page_main_button_more);
			}
			return;
			
		}  
		if (view.getId() == R.id.AddShoppingList) {
//			add a new shopping list
			ShoppingListFacade.getInstance().addShoppingList();
			return;
		}
		
		if (view.getId() == R.id.Shoppinglist_Row) {
			
			Cursor c = mCursor_shoppinglist;
			c.moveToPosition(((Integer)view.getTag()).intValue());
			
			ShoppingListFacade.getInstance().showShoppingList(
					c.getInt(c.getColumnIndex(ShoppingList.SL_ID)), 
					c.getString(c.getColumnIndex(ShoppingList.SL_NAME)));
			return;
		}
//		ImageButton btn = (ImageButton)view;
		Button btn = (Button)view;
		if (btn.getId() == R.id.Btn_Shoppinglist_More) {
			
			LinearLayout ll = (LinearLayout)btn.getParent();
//			ImageButton btn_ItemEdit = (ImageButton)ll.findViewById(R.id.Btn_Shoppinglist_Edit);
//			ImageButton btn_ItemDelete = (ImageButton)ll.findViewById(R.id.Btn_Shoppinglist_Delete);
//			ImageButton btn_ItemMore = (ImageButton)ll.findViewById(R.id.Btn_Shoppinglist_More);
			Button btn_Itemhide = (Button)ll.findViewById(R.id.Btn_Shoppinglist_Hide);
			Button btn_ItemDelete = (Button)ll.findViewById(R.id.Btn_Shoppinglist_Delete);
			Button btn_ItemMore = (Button)ll.findViewById(R.id.Btn_Shoppinglist_More);
			
			btn_ItemMore.setVisibility(Button.GONE);
			
			btn_Itemhide.setVisibility(Button.VISIBLE);
			btn_Itemhide.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.slide_left));
//			btn_ItemEdit.setVisibility(Button.VISIBLE);
//			btn_ItemEdit.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.slide_left));
			btn_ItemDelete.setVisibility(Button.VISIBLE);
			btn_ItemDelete.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.slide_left));
			
//			this.setItemPos(((Integer)(btn.getTag())).intValue());
//			cursor_item.requery();
			return;
		}
		if (btn.getId() == R.id.Btn_Shoppinglist_Hide) {
			LinearLayout ll = (LinearLayout)btn.getParent();
			Button btn_ItemHide = (Button)ll.findViewById(R.id.Btn_Shoppinglist_Hide);
			Button btn_ItemDelete = (Button)ll.findViewById(R.id.Btn_Shoppinglist_Delete);
			Button btn_ItemMore = (Button)ll.findViewById(R.id.Btn_Shoppinglist_More);
			
			btn_ItemHide.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.slide_right));
			btn_ItemHide.setVisibility(Button.GONE);
			btn_ItemDelete.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.slide_right));
			btn_ItemDelete.setVisibility(Button.GONE);
			btn_ItemMore.setVisibility(Button.VISIBLE);
			return;
		}
		if (btn.getId() == R.id.Btn_Shoppinglist_Delete) {
			
			final int item_id = ((Integer)(btn.getTag())).intValue();
			
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setMessage(R.string.page_delete_question).setCancelable(false)
			.setPositiveButton(R.string.button_yes, new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
		        	   ShoppingListFacade.getInstance().deleteShoppinglist(item_id);
		        	   dialog.cancel();
		        	   mCursor_shoppinglist.requery();
		           }
		       }).setNegativeButton(R.string.button_no, new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
		        	   dialog.cancel();
		           }
		       });
			AlertDialog alert = builder.create();
			alert.show();
			return;
		}
//		if (btn.getId() == R.id.Btn_Shoppinglist_Edit) {
//			
//			LinearLayout ll = (LinearLayout)btn.getParent();
//			ImageButton btn_ItemEdit = (ImageButton)ll.findViewById(R.id.Btn_Shoppinglist_Edit);
//			ImageButton btn_ItemDelete = (ImageButton)ll.findViewById(R.id.Btn_Shoppinglist_Delete);
//			ImageButton btn_ItemMore = (ImageButton)ll.findViewById(R.id.Btn_Shoppinglist_More);
//			EditText et_shoppinglist_name = (EditText)ll.findViewById(R.id.ET_Shoppinglist_Name);
//			Button btn_shoppinglist_ok = (Button)ll.findViewById(R.id.Btn_Shoppinglist_OK);
//			TextView tv_Item = (TextView)ll.findViewById(R.id.TV_Shoppinglist_Name);
//			
//			btn_ItemMore.setVisibility(Button.GONE);
//			btn_ItemEdit.setVisibility(Button.GONE);
//			btn_ItemDelete.setVisibility(Button.GONE);
//			tv_Item.setVisibility(TextView.GONE);
//			et_shoppinglist_name.setVisibility(EditText.VISIBLE);
//			btn_shoppinglist_ok.setVisibility(Button.VISIBLE);
//			
//			
//			Cursor c = mCursor_shoppinglist;
//			c.moveToPosition(((Integer)view.getTag()).intValue());
//			et_shoppinglist_name.setText(c.getString(c.getColumnIndex(ShoppingList.SL_NAME)));
//			
//			return;
//		}
		
	}
	
	private void fillShoppingLists() {
		
		if (mShowAll) {
			mCursor_shoppinglist = ShoppingListFacade.getInstance().getPersistence().fetchAllShoppingLists();
		} else {
			mCursor_shoppinglist = ShoppingListFacade.getInstance().getPersistence().fetchPartShoppingLists();
		}
		
        startManagingCursor(mCursor_shoppinglist);

//        String[] from = new String[] { ShoppingList.SL_NAME };
//        int[] to = new int[] { /*R.id.shoppinglists_item*/ android.R.id.text1};
//        
//        // Now create an array adapter and set it to display using our row
//        SimpleCursorAdapter sls =
//            new SimpleCursorAdapter(this, /*R.layout.shoppinglists_row*/ android.R.layout.simple_list_item_1, mCursor_shoppinglist, from, to);
//        ls_shoppingLists.setAdapter(sls);
        
        ls_shoppingLists.setAdapter(new ShoppinglistCursorAdapter(this, mCursor_shoppinglist));
	}
	
	private class ShoppinglistCursorAdapter extends ResourceCursorAdapter {
		
		public ShoppinglistCursorAdapter(Context context, Cursor cur) {
			super(context, R.layout.shoppinglists_row, cur);
		}
		
		@Override
		public View newView(Context context, Cursor cursor, ViewGroup parent) {
			
			LayoutInflater inflator = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			return inflator.inflate(R.layout.shoppinglists_row, parent, false);
		}

		@Override
		public void bindView(View view, Context context, Cursor cursor) {
			
			TextView tv_Item = (TextView)view.findViewById(R.id.TV_Shoppinglist_Name);
			tv_Item.setText(cursor.getString(cursor.getColumnIndex(ShoppingList.SL_NAME)));
			
			CheckBox cb_state = (CheckBox)view.findViewById(R.id.CB_Shoppinglist);
			int sl_id = cursor.getInt(cursor.getColumnIndex(ShoppingList.SL_ID));
//			cb_state.setTag(new Integer(item_id));
			boolean res = ShoppingListFacade.getInstance().getPersistence().isShoppinglistFinished(sl_id);
			if (res) {
				cb_state.setVisibility(CheckBox.VISIBLE);
				cb_state.setChecked(true);
			} else 
				cb_state.setVisibility(CheckBox.INVISIBLE);
			
//			EditText et_shoppinglist_name = (EditText)view.findViewById(R.id.ET_Shoppinglist_Name);
//			et_shoppinglist_name.setVisibility(EditText.GONE);
//			
//			Button btn_shoppinglist_ok = (Button)view.findViewById(R.id.Btn_Shoppinglist_OK);
//			btn_shoppinglist_ok.setTag(new Integer(item_id));
//			btn_shoppinglist_ok.setVisibility(Button.GONE);
//			btn_shoppinglist_ok.setOnClickListener(MainActivity.this);
			
//			ImageButton btn_ItemEdit = (ImageButton)view.findViewById(R.id.Btn_Shoppinglist_Edit);
			Button btn_ItemHide = (Button)view.findViewById(R.id.Btn_Shoppinglist_Hide);
			btn_ItemHide.setVisibility(Button.GONE);
			btn_ItemHide.setOnClickListener(MainActivity.this);
			
			Button btn_ItemEdit = (Button)view.findViewById(R.id.Btn_Shoppinglist_Edit);
			btn_ItemEdit.setTag(new Integer(cursor.getPosition()));
			btn_ItemEdit.setVisibility(Button.GONE);
			btn_ItemEdit.setOnClickListener(MainActivity.this);
			
//			ImageButton btn_ItemDelete = (ImageButton)view.findViewById(R.id.Btn_Shoppinglist_Delete);
			Button btn_ItemDelete = (Button)view.findViewById(R.id.Btn_Shoppinglist_Delete);
			btn_ItemDelete.setTag(new Integer(sl_id));
			btn_ItemDelete.setVisibility(Button.GONE);
			btn_ItemDelete.setOnClickListener(MainActivity.this);
			
//			ImageButton btn_ItemMore = (ImageButton)view.findViewById(R.id.Btn_Shoppinglist_More);
			Button btn_ItemMore = (Button)view.findViewById(R.id.Btn_Shoppinglist_More);
			btn_ItemMore.setOnClickListener(MainActivity.this);
			btn_ItemMore.setVisibility(Button.VISIBLE);

			view.setOnClickListener(MainActivity.this);
			view.setTag(new Integer(cursor.getPosition()));
		}
		
	}

}